import { addGoodsTypeAPI, deleteGoodsTypeAPI, editGoodsTypeAPI, fetchGoodsTypeDetailAPI, fetchGoodsTypeListAPI } from '@/api/business/goodsTypeAPI'

const actions = {

  /*
  * 获取货物类型分页数据的action
  *  */
  async fetchGoodsTypeListAction(context, params) {
    const { data } = await fetchGoodsTypeListAPI(params)
    context.commit('setGoodsTypeListMutation', data)
  },
  /*
  * 删除货物类型的action
  *  */
  async deleteGoodsTypeAction(context, id) {
    await deleteGoodsTypeAPI(id)
    return '货物类型删除成功'
  },
  /*
  * 添加货物类型的action
  *  */
  async addGoodsTypeAction(context, formData) {
    await addGoodsTypeAPI(formData)
    return '货物类型添加成功'
  },

  /*
  * 获取货物类型详情的action
  *
  *  */
  async fetchGoodsTypeDetailAction(context, id) {
    const { data } = await fetchGoodsTypeDetailAPI(id)
    return data
  },

  /*
  *
  *编辑货物类型的action
  *  */
  async editGoodsTypeAction(context, formData) {
    await editGoodsTypeAPI(formData)
    return '货物类型编辑成功'
  }
}

const mutations = {
  /*
  * 设置货物类型分页数据的mutation
  *  */
  setGoodsTypeListMutation(state, { items, counts }) {
    state.goodsTypeList = items
    state.counts = Number(counts)
  }
}
const state = () => {
  return {
    goodsTypeList: [],
    counts: 0
  }
}

export default {
  actions,
  mutations,
  state,
  namespaced: true
}
